﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YWTSSys.Model;
using YWTSSys.Library;
using YWTSSys.BLL;
using System.Data;
using System.Text;

namespace YWTSSys.Web.MachineSys.SysBasic
{
    public partial class ORoleEdit : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.修改 };
            }
        }
        protected override string DWZRef
        {
            get { return "ORoleList"; }
        }

        protected int RoleId { get { return GetRequestValue<int>("RoleId"); } }

        private ORoleInfo _objRole;
        protected ORoleInfo objRole {
            get
            {
                if (_objRole == null)
                    _objRole = ORoleController.GetById(RoleId);
                return _objRole;
            }
        }
        #region
        private DataTable _ALLMenuDT;
        private DataTable _AllFunDT;
        private DataTable ALLMenuDT
        {
            get
            {
                if (_ALLMenuDT == null)
                    _ALLMenuDT = OMenuController.GetAllOMenu();
                return _ALLMenuDT;
            }
        }

        private DataTable AllFunDT
        {
            get
            {
                if (_AllFunDT == null)
                    _AllFunDT = OMenuController.GetAllOMenuPermission();
                return _AllFunDT;
            }
        }

        private List<int> _AllRoleFun;
        private List<int> AllRoleFun
        {
            get
            {
                if (_AllRoleFun == null)
                {
                    _AllRoleFun = new List<int>();
                    List<ORoleFunctionInfo> list = ORoleController.GetRoleFunctionByRoleId(RoleId);
                    foreach (ORoleFunctionInfo item in list)
                    {
                        _AllRoleFun.Add(item.FunId);
                    }

                  
                }
                return _AllRoleFun;
            }
        }
        #endregion

        protected string ResponsePagePer()
        {
            StringBuilder sb = new StringBuilder();
            DataRow[] topMenu = ALLMenuDT.Select("ParMenuID=0");
            foreach (DataRow row in topMenu)
            {
                sb.AppendLine("<b>" + row["menuname"].ToString() + "</b><hr/>");
                DataRow[] subMenu = ALLMenuDT.Select("ParMenuID=" + row["MenuId"].ToString());
                foreach (DataRow subRow in subMenu)
                {
                    sb.AppendLine("<div class=\"perpagediv\">");
                    sb.AppendLine(" <label class=\"perpagelab\">" + subRow["MenuName"].ToString() + "：" + "</label>");

                    DataRow[] funDT = AllFunDT.Select("MenuID=" + subRow["MenuId"].ToString());
                    foreach (DataRow fun in funDT)
                    {
                        string strchecked="";
                        strchecked=AllRoleFun.Contains(Convert.ToInt32(fun["funid"].ToString()))?"checked":"";

                        sb.AppendLine("<label class=\"perpagetaglable\"><input name=\"funid\" type=\"checkbox\" value=\"" + fun["funid"].ToString() + "\" " + strchecked + " />" + fun["PermissionTag"].ToString() + "</label>");
                    }

                    sb.AppendLine("</div>");
                }
            }

            return sb.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "editrole")
            {
                addrole();
            }
        }

        private void addrole()
        {
            ORoleInfo obj = Request.SerializableObject<ORoleInfo>();
            string funids = GetRequestValue<string>("funid");
            if (ORoleController.Update(obj, funids))
                SuccessJson("成功修改角色信息");
            else
                ErrorJson("修改角色信息失败");
        }
    }
}